﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Drawing.Imaging;

namespace HPDMAS
{
    public partial class SecureImg : System.Web.UI.Page
    {
        private string chartData;
        private float left = 4;
        private float right;
        private float top;
        private float bottom;
        private float yCount;
        private float hspacing;
        private float vspacing;
        private Graphics g;
        private Rectangle r;
        private Color backColor;
        private Color foreColor;
        private Font baseFont;
        private Font legendFont;
        string key;

        private static Color GridColor = Color.FromArgb(120, 120, 250);

        private void Page_Load(object sender, System.EventArgs e)
        {
            key = null;
            if (key == null)
            {
                key = this.buildKey();
                Session["Code"] = key;
            }

            Bitmap StockBitMap = DrawChart(160, 60, key);

            StockBitMap.Save(Response.OutputStream, ImageFormat.Jpeg);

        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion

        public Bitmap DrawChart(int width, int height, string chartData)
        {
            Bitmap newBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(newBitmap);
            //Font myFont = new Font("Arial", 14, FontStyle.Italic | FontStyle.Bold); //Arial
            Font myFont = new Font("Courier", 32,
                DateTime.Now.Ticks % 2000 == 0
                    ? FontStyle.Bold
                : FontStyle.Italic | FontStyle.Bold);
            Rectangle r = new Rectangle(0, 0, width, height);
            Color myForeColor = Color.Navy;
            Color myBackColor = Color.White;

            this.DrawChart(g, r, myBackColor, myForeColor, myFont, chartData);
            return newBitmap;
        }

        public void DrawChart(Graphics g, Rectangle r, Color backColor, Color foreColor, Font baseFont, string chartData)
        {
            this.chartData = chartData;
            this.g = g;
            this.r = r;
            this.backColor = backColor;
            this.foreColor = foreColor;
            this.baseFont = baseFont;
            this.legendFont = new Font(baseFont.FontFamily, (baseFont.Size * 2 / 3), baseFont.Style | FontStyle.Bold);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            CalculateChartDimensions();
            DrawBackground();
            InternalDrawChart();
            g.DrawString(chartData, baseFont, new SolidBrush(foreColor), 1, 3);

        }

        private void CalculateChartDimensions()
        {
            right = r.Width - 5;
            top = 0;
            bottom = 30;

            yCount = 4;
            hspacing = (bottom - top) / yCount;
            vspacing = (right) / 5;
        }

        private void DrawBackground()
        {
            LinearGradientBrush b = new LinearGradientBrush(r, Color.AliceBlue, backColor, LinearGradientMode.Horizontal);
            g.FillRectangle(b, r);
            b.Dispose();
        }

        private void InternalDrawChart()
        {
            DrawGrid();
        }

        private void DrawGrid()
        {
            Pen gridPen = new Pen(GridColor);

            for (int i = 0; (vspacing * i) < right; i++)
            {
                float x = left + (vspacing * i);
                g.DrawLine(gridPen, x, top, x, bottom + (baseFont.Size * 1 / 30));
            }

            for (float i = bottom; i >= top; i -= hspacing)
            {
                g.DrawLine(gridPen, right, i + DateTime.Now.Second % 20, left - 4, i + 14);
            }
        }

        const string letters = "ABCDEFGHIJKLMNOPRSTUVWXYZabcdefghijklmnoprstuvwxyz0123456789";

        private char randLetter(Random rnd)
        {
            return letters[rnd.Next(letters.Length)];

        }

        private string buildKey()
        {
            Random rnd = new System.Random(unchecked((int)DateTime.Now.Ticks));
            string ret = "";
            for (int i = 0; i < 5; i++)
            {
                ret += randLetter(rnd);
            }

            return ret;
        }
    }
}